const { Students } = require("../models/studentsModels");
const { moveFiles, removeFiles } = require("../utils/upload");

//学生数据渲染
async function main(req, res, next) {
    const { type = 'name', message = '', pagesize = 3, currentpage = 1 } = req.query;
    //学生数据总条数
    const total = await Students.countDocuments();
    //总页数
    const totalpages = Math.ceil(total / pagesize);
    const data = await Students
        .find({ [type]: { $regex: message, $options: "$i" } })
        .limit(pagesize - 0)
        .skip((currentpage - 1) * pagesize)
        .populate('classes');//获取的是个对象
    try {
        res.send({
            data: { data, pagesize, totalpages, total, currentpage }
        });
    } catch (error) {
        console.log(error);
    }
}
//添加
async function addStudent(req, res, next) {
    // console.log(req.body.classes);
    let { name, age, classes, gender, stuImages } = req.body;
    const data = await Students.find({ name, age, classes, gender, stuImages });
    if (data.length == 0) {
        Students.create({ name, age, classes, gender, stuImages });
        if (stuImages) {
            moveFiles({
                fromPath: "./public/temp",
                toPath: "./public/images",
                filename: stuImages[0]
            })
            removeFiles("./public/temp");
        }
        res.send({
            status: 1
        })
    } else {
        res.send({
            status: 1,
            msg: "该学生已存在！"
        });
    }
}
//搜索
// async function searchStu(req, res, next) {
//     if (req.body.type == "name") {
//         let myname = req.body.message;
//         //模糊匹配name是匹配的条件   ：后面是匹配的内容
//         const data = await Students.find({ name: { $regex: myname, $options: "$i" } });
//         res.send(data);
//     } else if (req.body.type == "age") {
//         let myage = req.body.message;
//         const data = await Students.find({ age: { $regex: myage, $options: "$i" } });
//         res.send(data);
//     } else if (req.body.type == "classes") {
//         let myclasses = req.body.message;
//         const data = await Students.find({ classes: { $regex: myclasses, $options: "$i" } });
//         res.send(data);
//     } else if (req.body.type == "gender") {
//         let mygender = req.body.message;
//         const data = await Students.find({ gender: { $regex: mygender, $options: "$i" } });
//         res.send(data);
//     }
// }
//删除
async function deleteStu(req, res, next) {
    let { _id } = req.body;
    const data = await Students.find({ _id });
    try {
        if (data.length > 0) {
            Students.deleteOne({ _id }, function (err) { });
            res.send({
                status: 1,
                msg: "删除成功！"
            })
        } else {
            res.send({
                status: 0,
                msg: "删除失败！"
            })
        }
    } catch (error) {
        console.log(error);
        res.send({
            status: 0,
            msg: "删除失败！"
        })
    }
}
//修改
async function updataStu(req, res, next) {
    let { _id, name, age, gender } = req.body;
    const data = await Students.find({ _id });
    if (data.length > 0) {
        Students.updateMany({ _id }, { name, age, gender }, function (err) { })
        res.send({
            status: 1,
            msg: "修改成功！"
        })
    }
}


// let arr = [];
//查找学生
async function findStudent(req, res, next) {
    let { name, _id } = req.body;
    const data = await Students
        .find({ _id })
        .populate('classes');
    if (data.length > 0) {
        res.send({
            status: 1,
            data,
            message: "学生数据获取成功"
        })
    }
}
//添加课程
async function stuAddSubject(req, res, next) {
    let { name, subjects } = req.body;
    const data = await Students.findOne({ name });
    let boo = data.subjects.some(item => item == subjects);
    if (!boo) {
        await Students.updateMany({ name }, { $addToSet: { subjects } }, function (err) { });
        res.send({
            status: 1,
            message: "课程添加成功"
        })
    } else {
        res.send({
            status: 0,
            message: "该课程已存在"
        })
    }

}
//获取课程
async function stuGetSubject(req, res, next) {
    const { name } = req.body
    const data = await Students.findOne({ name });
    res.send({
        data,
        status: 1,
        message: "课程获取成功"
    })

}
// module.exports = { addStudent, main, searchStu, deleteStu, updataStu };
module.exports = { addStudent, main, deleteStu, updataStu, findStudent, stuAddSubject, stuGetSubject };